package com.ruoyi.web.controller.system;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.StatusMessageDetailInfo;
import com.ruoyi.system.domain.TmcEmergency;
import com.ruoyi.system.domain.TmcEmergencyAttach;
import com.ruoyi.system.domain.TmcMaintenanceAttach;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.system.service.ITmcEmergencyService;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 问题回收
 * Controller
 *
 * @author ruoyi
 * @date 2021-03-31
 */
@RestController
@RequestMapping("/system/emergency")
public class TmcEmergencyController extends BaseController {
    @Autowired
    private ITmcEmergencyService tmcEmergencyService;

    /**
     * 查询问题回收列表
     */
//    @PreAuthorize("@ss.hasPermi('system:emergency:list')")
    @PostMapping("/list")
    public TableDataInfo list(TmcEmergency tmcEmergency) {
        startPage();
        List<TmcEmergency> list = tmcEmergencyService.selectTmcEmergencyList(tmcEmergency);
        return getDataTable(list);
    } @GetMapping("/Pclist")
    public TableDataInfo Pclist(TmcEmergency tmcEmergency) {
        startPage();
        List<TmcEmergency> list = tmcEmergencyService.selectTmcEmergencyList(tmcEmergency);
        return getDataTable(list);
    }
    @PostMapping("/app/list")
    public List<TmcEmergency> applist(TmcEmergency tmcEmergency) {
        startPage();
        List<TmcEmergency> list = tmcEmergencyService.selectTmcEmergencyList(tmcEmergency);
        return list;
    }

    //查询紧急事件附件
    @PostMapping("/{id}")
    public String attach(@PathVariable("id") Long id) {

        return tmcEmergencyService.attach(id);
    }

    /**
     * 新增问题回收
     */
    @Log(title = "问题回收", businessType = BusinessType.INSERT)
    @PostMapping("/insertTmcEmergency")
    public Map add(@RequestBody Map map) {
        TmcEmergency tmcEmergency= new TmcEmergency();
        tmcEmergency.setPublicuserId((Integer) map.get("PublicUser_ID"));
        tmcEmergency.setDescription((String) map.get("Description"));
        tmcEmergency.setType(""+map.get("Type"));
        tmcEmergency.setLat(map.get("Lat")+"");
        tmcEmergency.setLon(map.get("Lon")+"");
        tmcEmergency.setLocation((String) map.get("Location"));
        tmcEmergencyService.insertTmcEmergency(tmcEmergency);
        HashMap map1=new HashMap();
        map1.put("ID", tmcEmergency.getId());
        map1.put("Status", 0);
        map1.put("ErrorMessage", "上传应急事件成功");
        return map1;
    }
    /**
     * 新增问题回收
     */
    @Log(title = "问题回收", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    public Map UploadEmergencyAttach(@RequestParam(value = "file") MultipartFile file, @RequestParam(value = "nId") Long nId) {
        try {
            String profile = RuoYiConfig.getProfile();
            byte[] bytes = file.getBytes();
            //文件夹
            Path path2 = Paths.get(profile+"//nId//" + nId);
            //具体文件
            Path path = Paths.get(profile+"//nId//" + nId + "//" + file.getOriginalFilename());
            File file1 = path.toFile();
            //创建文件夹
            if (!file1.exists()) {
                path2.toFile().mkdirs();
            }
            if (file1.isFile()) {
                return AjaxResult.error("文件上传失败,文件存在");
            } else {
                Files.write(path, bytes);//创建文件
                if (true) {
                    TmcEmergencyAttach tmcEmergencyAttach= new TmcEmergencyAttach();
                    tmcEmergencyAttach.setAttachUrl("profile/nId/" + nId + "/" + file.getOriginalFilename());
                    tmcEmergencyAttach.setEmergencyId(nId);
                    tmcEmergencyService.insertTmcEmergencyAttach(tmcEmergencyAttach);
                    HashMap map1=new HashMap();
                    map1.put("ID", tmcEmergencyAttach.getId());
                    map1.put("Status", 0);
                    map1.put("ErrorMessage", "上传应急事件成功");
                    return map1;
                } else {
                    if (file1.isFile()) {
                        file1.delete();
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    /**
     * 获取用户上报记录
     详细信息
     */
    @PostMapping("/selectTmcEmergencyByUId")
    public AjaxResult selectTmcEmergencyByUId(@RequestBody HashMap map)
    {
        TmcEmergency tmcEmergency= new TmcEmergency();
        tmcEmergency.setPublicuserId(Integer.valueOf(map.get("ID").toString()));
        return AjaxResult.success(tmcEmergencyService.selectTmcEmergencyByUId(tmcEmergency));
    }

    /**
     * 获取上报信息
     详细信息
     */
    @GetMapping("/selectTmcEmergencyById")
    public TmcEmergency getInfo(@RequestParam(value = "recordID") Long recordID)
    {
        return tmcEmergencyService.selectTmcEmergencyById(recordID);
    }
    /**
     * 获取用户上报记录
     详细信息
     */
    @PutMapping("/CompleteEmergency")
    public StatusMessageDetailInfo CompleteEmergency(@RequestParam("emergencyID") Long  emergencyID, @RequestParam("ID") Long id)
    {
        TmcEmergency tmcEmergency= new TmcEmergency();
        tmcEmergency.setId(emergencyID);
        tmcEmergency.setClientId(id);
        StatusMessageDetailInfo statusMessageDetailInfo = new StatusMessageDetailInfo();
        statusMessageDetailInfo.setState("1");
        if(tmcEmergencyService.updateclientById(tmcEmergency)>0)statusMessageDetailInfo.setErrorMessage("成功");
        return statusMessageDetailInfo;
    }
}
